package com.jacken.mars.controller;


import com.jacken.mars.commons.annotation.BaseAnnotationNote;
import com.jacken.mars.commons.constant.CommonConstants;
import com.jacken.mars.entity.req.RegisterUserReq;
import com.jacken.mars.entity.resp.R;
import com.jacken.mars.service.SysUserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wq
 * @version 1.0
 * @date 2020/12/15 17:39
 */
@RestController
@AllArgsConstructor
@RequestMapping("/admin")
public class SysUserLoginController extends BasicController {

    private final SysUserService sysUserService;

    /**
     * 采用拦截器的方式登录
     *
     * @param mobile 手机号
     * @param code   验证码
     * @return R
     */
    @PostMapping("/login")
    @BaseAnnotationNote(checkToken = false)
    public R login(String mobile, String code) {
        return R.success(sysUserService.login(mobile, code));
    }

    /**
     * 采用图片验证码方式登录
     *
     * @param mobile 手机号
     * @param code   验证码
     * @return
     */
    @PostMapping("v2/login")
    @BaseAnnotationNote(checkToken = false)
    public R v2Login(String mobile, String code, String uuid) {
        return R.success(sysUserService.v2Login(mobile, code, uuid));
    }

    /**
     * 采用账号密码登录
     *
     * @param mobile   手机号
     * @param password 密码
     * @param code     验证码
     * @param uuid     UUID
     * @return
     */
    @PostMapping("v3/login")
    @BaseAnnotationNote(checkToken = false)
    public R v2Login(String mobile, String password, String code, String uuid) {
        return R.success(sysUserService.v3Login(mobile, password, code, uuid));
    }


    /**
     * 用户注册
     *
     * @param req
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody RegisterUserReq req) {
        sysUserService.register(req);
        return R.success("注册成功");

    }


    /**
     * 获取用户信息
     *
     * @param request request
     * @return
     */
    @GetMapping("/getUserInfo")
    @BaseAnnotationNote(checkToken = false)
    public R getUserInfo(HttpServletRequest request) {
        return R.success(sysUserService.userInfo(request.getHeader(CommonConstants.TOKEN)));
    }

    /**
     * 退出
     *
     * @return R
     */
    @GetMapping("logout")
    @BaseAnnotationNote(checkToken = false)
    public R logout(HttpServletRequest request) {
        sysUserService.logout(request.getHeader(CommonConstants.TOKEN));
        return R.success();
    }

    /**
     * 获取短信验证码
     *
     * @param mobile 电话号码
     * @return R
     */
    @GetMapping("/sendMessage")
    @BaseAnnotationNote(checkToken = false)
    public R getSmsCode(String mobile) {
        sysUserService.sendSmsCode(mobile);
        return R.success();
    }
}
